package com.birdboot.controller;

import com.birdboot.entity.User;
import com.birdboot.http.HttpServletRequest;
import com.birdboot.http.HttpServletResponse;

import java.io.*;

public class UserController {
    //用于表示存储所有用户文件的users目录
    private static File userDir;
    static{
            /* File baseDir = new File(UserController.class.
                    getClassLoader().getResource(".").toURI());
            userDir = new File(baseDir,"/localFile/user");*/
        userDir = new File("./localFile/user");
        if(!userDir.exists()){
            userDir.mkdirs();
        }
    }
    /**
     * 处理用户注册
     * @param request
     * @param response
     */
    public void reg(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr =request.getParameter("age");
        System.out.println(username+", "+password+", "+nickname+", "+ageStr);

        if(username==null || username.isEmpty() ||
                password==null || password.isEmpty() ||
                nickname==null || nickname.isEmpty() ||
                ageStr==null || ageStr.isEmpty() ||
                !ageStr.matches("\\d+")){

            response.sendRedirect("/reg_info_error.html");
            return;
        }

        int age = Integer.parseInt(ageStr);
        User user = new User(username,password,nickname,age);

        File file = new File(userDir,username+".obj");
        if(file.exists()){
            response.sendRedirect("/have_user.html");
            return;
        }

        try(FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
        ){
            oos.writeObject(user);
            response.sendRedirect("/reg_success.html");
            return;
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void login(HttpServletRequest request,HttpServletResponse response){
        //1获取登录信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //必要验证
        if(username==null || username.isEmpty() ||
                password==null || password.isEmpty()
        ){
            response.sendRedirect("/login_info_error.html");
            return;
        }
        //根据该登录用户的名字去定位users下该用户的注册信息
        File file = new File(userDir,username+".obj");
        //判断该文件是否存在,不存在则说明该用户没有注册过
        if(file.exists()){
            //将该用户曾经的注册信息读取出来用于比较密码
            try(
                    FileInputStream fis = new FileInputStream(file);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ){
                User user = (User)ois.readObject();//读取注册信息
                //比较本次登录的密码是否与注册时该用户输入的密码一致
                if(password.equals(user.getPassword())){
                    //密码一致则登录成功
                    response.sendRedirect("/login_success.html");
                    return;
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        //如果程序可以执行到这里,则说明要么是用户名没输入对,要么是密码没有输入对.都属于登录失败
        response.sendRedirect("/login_fail.html");

    }
}
